
package com.wyzz.hr.bo.impl;

import com.wyzz.hr.base.TreeNode;
import com.wyzz.hr.bo.IDepartmentBO;
import com.wyzz.hr.bo.IEmployeeBo;
import com.wyzz.hr.bo.IPositionBaseBo;
import com.wyzz.hr.bo.IPositionBo;
import com.wyzz.hr.dao.IPositionDao;
import com.wyzz.hr.pojo.Department;
import com.wyzz.hr.pojo.Employee;
import com.wyzz.hr.pojo.Position;
import com.wyzz.hr.pojo.PositionBase;
import com.wyzz.hr.spring.SpringBeanFactory;
import com.wyzz.hr.util.ObjectUtil;
import com.wyzz.hr.util.StringUtil;

import java.util.*;
import java.util.Map.Entry;

import org.apache.axis.utils.StringUtils;
import org.hibernate.FetchMode;
import org.hibernate.criterion.*;


public class PositionBoImpl implements IPositionBo {

	public PositionBoImpl() {
	}

	public IPositionDao getPositionDao() {
		return positionDao;
	}

	public void setPositionDao(IPositionDao positionDao) {
		this.positionDao = positionDao;
	}

	public Position getPosById(String posId, String fetch[]) {
		return (Position) positionDao.loadObject(
				com.wyzz.hr.pojo.Position.class, posId, fetch,
				new boolean[] { true });
	}

	public Position getPositionById(String posId) {
		DetachedCriteria dc = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Position.class);
		dc.add(Restrictions.eq(Position.PROP_ID, posId));
		dc.createAlias(Position.PROP_POSITION_EMP_NO,
				Position.PROP_POSITION_EMP_NO, 1);
		dc.createAlias(Position.PROP_POSITION_PB_ID,
				Position.PROP_POSITION_PB_ID, 1);
		dc.createAlias((new StringBuilder()).append(
				Position.PROP_POSITION_PB_ID).append(".").append(
				PositionBase.PROP_PB_DEPT_ID).toString(), (new StringBuilder())
				.append(Position.PROP_POSITION_PB_ID).append(".").append(
						PositionBase.PROP_PB_DEPT_ID).toString(), 1);
		List list = positionDao.findByCriteria(dc);
		if (list == null || list.isEmpty())
			return null;
		else
			return (Position) list.get(0);
	}

	public Position getPositionByPbId(String pbId) {
		DetachedCriteria dc = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Position.class);
		dc.createAlias(Position.PROP_POSITION_PB_ID,
				Position.PROP_POSITION_PB_ID, 1);
		dc.add(Restrictions.eq((new StringBuilder()).append(
				Position.PROP_POSITION_PB_ID).append(".").append(
				PositionBase.PROP_ID).toString(), pbId));
		List list = positionDao.findByCriteria(dc);
		if (list == null || list.isEmpty())
			return null;
		else
			return (Position) list.get(0);
	}

	public boolean savePos(Position pos) {
		positionDao.saveOrupdate(pos);
		return true;
	}

	public boolean savePosList(List newList) {
		try {
			positionDao.saveOrupdate(newList);
		} catch (RuntimeException e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}

	public List findAllActivePosition() {
		String hql = "select pos.id,pos.positionParentId.id,pos.positionPbId.pbInCharge,pos.positionPbId.pbName,dept.id,pos.positionEmpNo.id,pos.positionEmpNo.empName,pos.positionEmpNo.empGender,pos.positionTakeNum from Position as pos left join pos.positionEmpNo left join pos.positionParentId left join pos.positionPbId,Department as dept where pos.positionPbId.pbDeptId = dept.id and pos.positionPbId.pbStatus = 1 order by dept.departmentSortId,pos.positionPbId.pbSortId";
		List result = positionDao.exeHqlList(hql);
		List positionList = new ArrayList();
		for (int i = 0; i < result.size(); i++) {
			Object obj[] = (Object[]) (Object[]) result.get(i);
			Position pos = new Position(obj[0].toString());
			if (obj[1] != null)
				pos.setPositionParentId(new Position(obj[1].toString()));
			PositionBase pb = new PositionBase();
			pb.setPbInCharge((Integer) obj[2]);
			pb.setPbName(obj[3].toString());
			pb.setPbDeptId(new Department(obj[4].toString()));
			pos.setPositionPbId(pb);
			if (obj[5] != null) {
				Employee emp = new Employee(obj[5].toString());
				emp.setEmpName(obj[6].toString());
				emp.setEmpGender((Integer) obj[7]);
				pos.setPositionEmpNo(emp);
			}
			pos.setPositionTakeNum((Integer) obj[8]);
			positionList.add(pos);
		}

		return positionList;
	}

	public Position getRespPosOfDept(String deptId) {
		DetachedCriteria dc = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Position.class);
		dc.createAlias(Position.PROP_POSITION_PB_ID, "positionPbId", 1);
		dc.createAlias(Position.PROP_POSITION_EMP_NO, "positionEmpNo", 1);
		dc.add(Restrictions.eq((new StringBuilder()).append("positionPbId.")
				.append(PositionBase.PROP_PB_DEPT_ID).toString(),
				new Department(deptId)));
		dc.add(Restrictions.eq((new StringBuilder()).append("positionPbId.")
				.append(PositionBase.PROP_PB_IN_CHARGE).toString(),
				new Integer(1)));
		List posList = positionDao.findByCriteria(dc);
		if (posList.size() == 0)
			return null;
		else
			return (Position) posList.get(0);
	}

	public List getPosListOfDept(String deptId) {
		DetachedCriteria dc = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Position.class);
		dc.createAlias(Position.PROP_POSITION_PB_ID, "positionPbId", 1);
		dc.createAlias(Position.PROP_POSITION_EMP_NO, "positionEmpNo", 1);
		dc.add(Restrictions.eq((new StringBuilder()).append("positionPbId.")
				.append(PositionBase.PROP_PB_DEPT_ID).toString(),
				new Department(deptId)));
		dc.addOrder(Order.asc("positionPbId.pbSortId"));
		List posList = positionDao.findByCriteria(dc);
		return posList;
	}

	public List getRespPosListOfDept(String deptIds[]) {
		DetachedCriteria dc = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Position.class);
		dc.createAlias(Position.PROP_POSITION_PB_ID, "positionPbId", 1);
		dc.add(Restrictions.in((new StringBuilder()).append("positionPbId.")
				.append(PositionBase.PROP_PB_DEPT_ID).append(".id").toString(),
				deptIds));
		dc.add(Restrictions.eq((new StringBuilder()).append("positionPbId.")
				.append(PositionBase.PROP_PB_IN_CHARGE).toString(),
				new Integer(1)));
		List posList = positionDao.findByCriteria(dc);
		return posList;
	}

	public List getPosListOfEmps(List empList) {
		DetachedCriteria dc = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Position.class);
		dc.createAlias(Position.PROP_POSITION_PB_ID, "positionPbId", 1);
		dc.add(Restrictions.in(Position.PROP_POSITION_EMP_NO, empList));
		List posList = positionDao.findByCriteria(dc);
		return posList;
	}

	public List getAllParentPos() {
		String hql = "select distinct positionParentId.id from Position pos where pos.positionParentId is not null";
		List parentPosList = positionDao.exeHqlList(hql);
		return parentPosList;
	}

	public boolean isSupPos(String supPosNo, String subPosNo) {
		return ObjectUtil.contains(getAllSubPosIds(0, supPosNo), subPosNo);
	}

	public Position[] getAllSupPosIds(int sign, Department dept) {
		Position pos = getRespPosOfDept(dept.getId());
		return getAllSupPosIds(sign, pos.getId());
	}

	public Position[] getAllSupPosIds(int sign, Employee emp) {
		Position pos = getPosByEmpNo(emp.getId(), new String[0]);
		return getAllSupPosIds(sign, pos.getId());
	}

	public Position[] getAllSupPosIds(int sign, String posId) {
		DetachedCriteria dc = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Position.class);
		dc.createAlias(Position.PROP_POSITION_EMP_NO, "positionEmpNo", 1);
		dc.createAlias((new StringBuilder()).append("positionEmpNo.").append(
				Employee.PROP_EMP_DEPT_NO).toString(), "empDeptNo", 1);
		dc.createAlias(Position.PROP_POSITION_PB_ID, "positionPbId", 1);
		List allPosList = positionDao.findByCriteria(dc);
		Map allPosMap = new HashMap();
		Position pos;
		for (Iterator i$ = allPosList.iterator(); i$.hasNext(); allPosMap.put(
				pos.getId(), pos))
			pos = (Position) i$.next();

		Position currPos = (Position) allPosMap.get(posId);
		if (currPos == null)
			return null;
		List supPosQueue = new LinkedList();
		supPosQueue.add(currPos);
		do {
			if (currPos.getPositionParentId() == null)
				break;
			currPos = (Position) allPosMap.get(currPos.getPositionParentId()
					.getId());
			if (currPos == null)
				break;
			supPosQueue.add(currPos);
		} while (true);
		if (sign == 0)
			supPosQueue.remove(0);
		return (Position[]) supPosQueue
				.toArray(new Position[supPosQueue.size()]);
	}

	public String[] getAllSubPosIds(int sign, String posNo) {
		List subPosList = getAllSubPositions(null, new String[] { posNo });
		if (subPosList == null)
			return null;
		if (sign == 0)
			subPosList.remove(0);
		List subPosNoList = new ArrayList();
		Position tempDept;
		for (Iterator i$ = subPosList.iterator(); i$.hasNext(); subPosNoList
				.add(tempDept.getId()))
			tempDept = (Position) i$.next();

		if (subPosNoList.size() == 0)
			return null;
		else
			return (String[]) subPosNoList.toArray(new String[subPosNoList
					.size()]);
	}

	public String[] getAllSubEmpIds(int sign, String posNo) {
		List subPosList = getAllSubPositions(
				new String[] { Position.PROP_POSITION_EMP_NO },
				new String[] { posNo });
		if (subPosList == null)
			return null;
		if (sign == 0)
			subPosList.remove(0);
		List subEmpNoList = new ArrayList();
		Iterator i$ = subPosList.iterator();
		do {
			if (!i$.hasNext())
				break;
			Position tempDept = (Position) i$.next();
			if (tempDept.getPositionEmpNo() != null)
				subEmpNoList.add(tempDept.getPositionEmpNo().getId());
		} while (true);
		if (subEmpNoList.size() == 0)
			return null;
		else
			return (String[]) subEmpNoList.toArray(new String[subEmpNoList
					.size()]);
	}

	public List getAllSubPositions(String fetch[], String posNos[]) {
		DetachedCriteria dc = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Position.class);
		if (fetch != null) {
			for (int i = 0; i < fetch.length; i++)
				dc.setFetchMode(fetch[i], FetchMode.JOIN);

		}
		List allPosList = positionDao.findByCriteria(dc);
		Iterator iter = allPosList.iterator();
		List subPosQueue = new LinkedList();
		do {
			if (!iter.hasNext())
				break;
			Position tempPos = (Position) iter.next();
			if (ObjectUtil.contains(posNos, tempPos.getId())) {
				subPosQueue.add(tempPos);
				iter.remove();
			}
		} while (true);
		if (subPosQueue.size() == 0)
			return null;
		Position parentPos = null;
		label0: for (int i = 0; i < subPosQueue.size(); i++) {
			parentPos = (Position) subPosQueue.get(i);
			iter = allPosList.iterator();
			do {
				if (!iter.hasNext())
					continue label0;
				Position tempPos = (Position) iter.next();
				if (tempPos.getPositionParentId() != null
						&& tempPos.getPositionParentId().getId().equals(
								parentPos.getId())) {
					subPosQueue.add(tempPos);
					iter.remove();
				}
			} while (true);
		}

		return subPosQueue;
	}

	private List removeBlankPosIds(List allPosList) {
		Map blankPosMap = new HashMap();
		Map empPosMap = new HashMap();
		List empPosList = new ArrayList();
		Position pos;
		for (Iterator i$ = allPosList.iterator(); i$.hasNext(); empPosMap.put(
				pos.getId(), pos)) {
			pos = (Position) i$.next();
			if (pos.getPositionParentId() != null
					&& pos.getPositionEmpNo() == null)
				blankPosMap.put(pos.getId(), pos);
			else
				empPosList.add(pos);
		}

		for (Iterator i$ = empPosList.iterator(); i$.hasNext();) {
			pos = (Position) i$.next();
			if (pos.getPositionParentId() != null) {
				Position parent = (Position) empPosMap.get(pos
						.getPositionParentId().getId());
				if (parent == null)
					parent = (Position) blankPosMap.get(pos
							.getPositionParentId().getId());
				while (parent != null
						&& blankPosMap.get(parent.getId()) != null) {
					parent = parent.getPositionParentId();
					if (parent != null)
						pos.setPositionParentId(parent);
				}
			}
		}

		return empPosList;
	}

	public Map getPerfEvalPosIds(int sign, int perfPlanEmpChoose,
			String posIds[]) {
		if (posIds == null || posIds.length == 0)
			return null;
		DetachedCriteria dc = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Position.class);
		dc.createAlias(Position.PROP_POSITION_EMP_NO, "positionEmpNo", 1);
		dc.createAlias(Position.PROP_POSITION_PARENT_ID, "positionParentId", 1);
		dc.createAlias((new StringBuilder()).append("positionEmpNo.").append(
				Employee.PROP_EMP_DEPT_NO).toString(), "empDeptNo", 1);
		dc.createAlias(Position.PROP_POSITION_PB_ID, "positionPbId", 1);
		List allPosList = positionDao.findByCriteria(dc);
		allPosList = removeBlankPosIds(allPosList);
		Map allPosMap = new HashMap();
		Position pos;
		for (Iterator i$ = allPosList.iterator(); i$.hasNext(); allPosMap.put(
				pos.getId(), pos))
			pos = (Position) i$.next();

		Map resultPosMap = new HashMap();
		String arr$[] = posIds;
		int len$ = arr$.length;
		for (int i = 0; i < len$; i++) {
			String posId = arr$[i];
			Position currPos = (Position) allPosMap.get(posId);
			if (currPos == null) {
				resultPosMap.put(posId, null);
				continue;
			}
			List supPosQueue = new LinkedList();
			supPosQueue.add(currPos);
			if (perfPlanEmpChoose < 3) {
				int suplevel = 0;
				do {
					if (currPos.getPositionParentId() == null)
						break;
					currPos = (Position) allPosMap.get(currPos
							.getPositionParentId().getId());
					if (currPos == null)
						break;
					supPosQueue.add(currPos);
				} while (++suplevel != perfPlanEmpChoose);
			} else {
				if (currPos.getPositionParentId() != null) {
					Position parentPos = (Position) allPosMap.get(currPos
							.getPositionParentId().getId());
					if (parentPos != null) {
						supPosQueue.add(parentPos);
						Iterator i$ = allPosMap.entrySet().iterator();
						do {
							if (!i$.hasNext())
								break;
							java.util.Map.Entry entry = (java.util.Map.Entry) i$
									.next();
							if (((Position) entry.getValue())
									.getPositionParentId().equals(parentPos)
									&& !((Position) entry.getValue())
											.equals(currPos))
								supPosQueue.add(entry.getValue());
						} while (true);
					}
				}
				Iterator i$ = allPosMap.entrySet().iterator();
				do {
					if (!i$.hasNext())
						break;
					Entry entry = (Entry) i$.next();
					if (((Position) entry.getValue()).getPositionParentId() != null
							&& ((Position) entry.getValue())
									.getPositionParentId().equals(currPos)) {
						Position position = (Position) entry.getValue();
						supPosQueue.add(entry.getValue());
					}
				} while (true);
			}
			if (sign == 0)
				supPosQueue.remove(0);
			resultPosMap.put(posId, ((Object) (supPosQueue
					.toArray(new Position[supPosQueue.size()]))));
		}

		return resultPosMap;
	}

	public Position getPosByEmpNo(String empNo, String fetch[]) {
		Employee emp = new Employee(empNo);
		if (fetch != null && fetch.length == 0)
			fetch = null;
		Position pos = (Position) positionDao.loadObjectByName(
				com.wyzz.hr.pojo.Position.class, Position.PROP_POSITION_EMP_NO,
				emp, fetch, new boolean[0]);
		if (pos == null)
			return null;
		else
			return pos;
	}

	public Employee getEmpWithPos(String empNo, String fetch[]) {
		Position pos = getPosByEmpNo(empNo, fetch);
		if (pos == null)
			return null;
		Employee emp = pos.getPositionEmpNo();
		if (emp == null) {
			return null;
		} else {
			emp.setPosition(pos);
			return emp;
		}
	}

	public List getDirectSubPos(String posId) {
		DetachedCriteria dc = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Position.class);
		dc.createAlias(Position.PROP_POSITION_EMP_NO, "positionEmpNo", 1);
		dc.add(Restrictions.eq((new StringBuilder()).append(
				Position.PROP_POSITION_PARENT_ID).append(".id").toString(),
				posId));
		List posList = positionDao.findByCriteria(dc);
		return posList;
	}

	public List getPosListByPB(PositionBase pb) {
		DetachedCriteria dc = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Position.class);
		dc.createAlias(Position.PROP_POSITION_EMP_NO, "positionEmpNo", 1);
		dc.add(Restrictions.eq(Position.PROP_POSITION_PB_ID, pb));
		List posList = positionDao.findByCriteria(dc);
		return posList;
	}

	public String delPosition(String posId, boolean delPB) {
		String msgPosNoExist = "({0})\u804C\u4F4D\u4E0D\u5B58\u5728\uFF0C\u8BF7\u5237\u65B0\u9875\u9762\u540E\u91CD\u8BD5\uFF01";
		String msgParamError = "\u53C2\u6570\u9519\u8BEF\uFF0C\u7A0D\u540E\u91CD\u8BD5\uFF01";
		String msgEmpExist = "({0})\u804C\u4F4D\u6709\u5458\u5DE5\uFF0C\u4E0D\u80FD\u5220\u9664\uFF01";
		String msgDeptNoResp = "({0})\u804C\u4F4D\u6240\u5728\u90E8\u95E8\u53EA\u6709\u5F53\u524D\u8D1F\u8D23\u804C\u4F4D\uFF0C\u4E0D\u80FD\u5220\u9664\uFF01";
		Position position = getPosById(posId, new String[] {
				Position.PROP_POSITION_PB_ID,
				(new StringBuilder()).append(Position.PROP_POSITION_PB_ID)
						.append(".").append(PositionBase.PROP_PB_DEPT_ID)
						.toString() });
		if (position == null)
			return StringUtil.message(msgPosNoExist, new Object[] { position
					.getPositionPbId().getPbName() });
		if (position.getPositionEmpNo() != null)
			return StringUtil.message(msgEmpExist, new Object[] { position
					.getPositionPbId().getPbName() });
		if (!delPB
				&& position.getPositionPbId().getPbInCharge().intValue() == 1) {
			String hql = (new StringBuilder()).append(
					"select count(*) from Position where positionPbId.id = '")
					.append(position.getPositionPbId().getId()).append("'")
					.toString();
			List result = positionDao.exeHqlList(hql);
			if (result.isEmpty())
				return msgParamError;
			if (result != null && result.get(0) != null
					&& ((Long) (Long) result.get(0)).longValue() < 2L)
				return StringUtil
						.message(msgDeptNoResp, new Object[] { position
								.getPositionPbId().getPbName() });
		}
		List subPosition = getDirectSubPos(posId);
		if (subPosition != null && subPosition.size() > 0) {
			String subMsg = null;
			Iterator i$ = subPosition.iterator();
			do {
				if (!i$.hasNext())
					break;
				Position posIn = (Position) i$.next();
				subMsg = delPosition(posIn.getId(), delPB);
			} while (subMsg == null);
			if (subMsg != null)
				return subMsg;
		}
		try {
			String hql = (new StringBuilder()).append(
					"delete from AuthorityPos where apPos.id='").append(posId)
					.append("'").toString();
			positionDao.exeHql(hql);
			hql = (new StringBuilder()).append(
					"delete from Position where id='").append(posId)
					.append("'").toString();
			positionDao.exeHql(hql);
		} catch (RuntimeException e) {
			e.printStackTrace();
			return "\u5220\u9664\u5931\u8D25!";
		}
		return null;
	}

	public List getPositionTreeListNode() {
		IDepartmentBO deptbo = (IDepartmentBO) SpringBeanFactory
				.getBean("departmentBO");
		IPositionBo positionBo = (IPositionBo) SpringBeanFactory
				.getBean("positionBo");
		List nodeList = new ArrayList();
		List deptList = deptbo.FindAllDepartment();
		Iterator i$ = deptList.iterator();
		do {
			if (!i$.hasNext())
				break;
			Department dept = (Department) i$.next();
			if (dept.getDepartmentParentId() == null)
				dept.setDepartmentParentId(new Department("0"));
			TreeNode node = null;
			if (dept.getDepartmentStatus().intValue() == 1) {
				node = new TreeNode(dept.getId(), dept.getDepartmentName(),
						dept.getDepartmentParentId().getId(), dept
								.getDepartmentCate().intValue());
				nodeList.add(node);
			}
		} while (true);
		List positionList = findAllActivePosition();
		String nodeName = null;
		for (int i = 0; positionList != null && i < positionList.size(); i++) {
			Position pos = (Position) positionList.get(i);
			int isResp = 0;
			if (pos.getPositionPbId().getPbInCharge().intValue() == 1) {
				pos.setPositionParentId(new Position(pos.getPositionPbId()
						.getPbDeptId().getId()));
				isResp = 1;
			}
			int nodeType;
			if (pos.getPositionEmpNo() != null) {
				nodeName = (new StringBuilder()).append(
						pos.getPositionPbId().getPbName()).append("(").append(
						pos.getPositionEmpNo().getEmpName()).append(")")
						.toString();
				if (pos.getPositionEmpNo().getEmpGender().intValue() == 1)
					nodeType = 5;
				else
					nodeType = 6;
			} else {
				nodeName = (new StringBuilder()).append(
						pos.getPositionPbId().getPbName()).append(
						"(\u7A7A\u7F3A)").toString();
				nodeType = 7;
			}
			TreeNode positionNode = new TreeNode(pos.getId(), nodeName, pos
					.getPositionParentId().getId(), nodeType, isResp);
			nodeList.add(i, positionNode);
		}

		System.out.println(nodeList.size());
		return nodeList;
	}

	public List getChangeSupTreeListNode(String posId) {
		String fetch[] = { Position.PROP_POSITION_PB_ID };
		Position position = getPosById(posId, fetch);
		List posList = getPosListOfDept(position.getPositionPbId()
				.getPbDeptId().getId());
		Position rootPos = null;
		Iterator i$ = posList.iterator();
		do {
			if (!i$.hasNext())
				break;
			Position pos = (Position) i$.next();
			if (pos.getPositionPbId().getPbInCharge().intValue() == 1)
				rootPos = pos;
		} while (true);
		List nodeList = convertPosToNodeList(posList, rootPos);
		return nodeList;
	}

	private List convertPosToNodeList(List posList, Position rootPos) {
		List nodeList = new ArrayList();
		String nodeName = null;
		TreeNode positionNode;
		for (Iterator i$ = posList.iterator(); i$.hasNext(); nodeList
				.add(positionNode)) {
			Position pos = (Position) i$.next();
			if (pos.getPositionEmpNo() != null)
				nodeName = (new StringBuilder()).append(
						pos.getPositionPbId().getPbName()).append("(").append(
						pos.getPositionEmpNo().getEmpName()).append(")")
						.toString();
			else
				nodeName = (new StringBuilder()).append(
						pos.getPositionPbId().getPbName()).append(
						"(\u7A7A\u7F3A)").toString();
			if (rootPos.getId().equals(pos.getId()))
				pos.setPositionParentId(new Position("0"));
			positionNode = new TreeNode(pos.getId(), nodeName, pos
					.getPositionParentId().getId(), 3);
		}

		return nodeList;
	}

	public Position getPosInCharge(String posId) {
		Position pos = getPosById(posId,
				new String[] { Position.PROP_POSITION_PB_ID });
		List deptPosList = getPosListOfDept(pos.getPositionPbId().getPbDeptId()
				.getId());
		for (Iterator i$ = deptPosList.iterator(); i$.hasNext();) {
			Position posInDept = (Position) i$.next();
			if (posInDept.getPositionPbId().getPbInCharge().intValue() == 1)
				return posInDept;
		}

		return null;
	}

	public String batchSetEmpPos(String parentPosId, String pbId, String ids[]) {
		if (ids == null || ids.length == 0)
			return "SUCC";
		IPositionBaseBo pbBo = (IPositionBaseBo) SpringBeanFactory
				.getBean("positionBaseBo");
		IEmployeeBo empBo = (IEmployeeBo) SpringBeanFactory.getBean("empBo");
		List empList = empBo.searchEmpArray(ids);
		if (empList.size() == 0)
			return "\u6570\u636E\u72B6\u6001\u9519\u8BEF\uFF0C\u8BF7\u5237\u65B0\u9875\u9762\u540E\u91CD\u8BD5\uFF01";
		List posList = getPosListOfEmps(empList);
		for (int i = 0; i < posList.size(); i++) {
			Position pos = (Position) posList.get(i);
			pos.setPositionEmpNo(null);
		}

		positionDao.saveOrupdate(posList);
		DetachedCriteria dc = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Position.class);
		dc.add(Restrictions.eq((new StringBuilder()).append(
				Position.PROP_POSITION_PB_ID).append(".id").toString(), pbId));
		if (!StringUtils.isEmpty(parentPosId))
			dc.add(Restrictions.eq((new StringBuilder()).append(
					Position.PROP_POSITION_PARENT_ID).append(".id").toString(),
					parentPosId));
		else
			dc.add(Restrictions.isNull(Position.PROP_POSITION_PARENT_ID));
		dc.add(Restrictions.isNull(Position.PROP_POSITION_EMP_NO));
		List nullPosList = positionDao.findByCriteria(dc);
		List newPosList = new ArrayList();
		if (empList.size() <= nullPosList.size()) {
			for (int i = 0; i < empList.size(); i++) {
				Employee emp = (Employee) empList.get(i);
				Position pos = (Position) nullPosList.get(i);
				if (parentPosId != null && !parentPosId.equals(""))
					pos.setPositionParentId(new Position(parentPosId));
				else
					pos.setPositionParentId(null);
				pos.setPositionEmpNo(emp);
			}

		} else {
			for (int i = 0; i < nullPosList.size(); i++) {
				Position pos = (Position) nullPosList.get(i);
				Employee emp = (Employee) empList.get(i);
				pos.setPositionEmpNo(emp);
			}

			for (int i = nullPosList.size(); i < empList.size(); i++) {
				Employee emp = (Employee) empList.get(i);
				Position pos = new Position();
				pos.setPositionPbId(new PositionBase(pbId));
				pos.setPositionParentId(StringUtils.isEmpty(parentPosId) ? null
						: new Position(parentPosId));
				pos.setPositionTakeNum(new Integer(1));
				pos.setPositionEmpNo(emp);
				newPosList.add(pos);
			}

		}
		positionDao.saveOrupdate(nullPosList);
		positionDao.saveOrupdate(newPosList);
		return "SUCC";
	}

	public Map getPosDBMap() {
		String hql = "select emp.empDistinctNo, pos.id from Position pos left join pos.positionEmpNo emp";
		List list = positionDao.exeHqlList(hql);
		Map empPosMap = new HashMap();
		for (int i = 0; list != null && i < list.size(); i++) {
			String obj[] = (String[]) (String[]) list.get(i);
			empPosMap.put(obj[0], obj[1]);
		}

		return empPosMap;
	}

	public List getDBPosList() {
		DetachedCriteria dc = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Position.class);
		dc.createAlias(Position.PROP_POSITION_PB_ID, "positionPbId", 1);
		dc.createAlias(Position.PROP_POSITION_EMP_NO, "positionEmpNo", 1);
		List dbPosList = positionDao.findByCriteria(dc);
		return dbPosList;
	}

	public Map getAllDeptRespPosMap() {
		Map respPosMap = new HashMap();
		List dbPosList = getDBPosList();
		for (int i = 0; dbPosList != null && i < dbPosList.size(); i++) {
			Position pos = (Position) dbPosList.get(i);
			if (pos.getPositionPbId().getPbInCharge().intValue() == 1)
				respPosMap
						.put(pos.getPositionPbId().getPbDeptId().getId(), pos);
		}

		return respPosMap;
	}

	public boolean parentPosInList(List posList, Position pos) {
		for (int i = 0; posList != null && i < posList.size(); i++) {
			Position tempPos = (Position) posList.get(i);
			if (tempPos == pos.getPositionParentId())
				return true;
		}

		return false;
	}

	public Object putEmpToPB(List dbPosList, List filePosList, Employee emp,
			PositionBase currPB) {
		int count = 0;
		Position emptyPos = null;
		for (int i = 0; i < dbPosList.size(); i++) {
			Position pos = (Position) dbPosList.get(i);
			if (!pos.getPositionPbId().getId().equals(currPB.getId()))
				continue;
			if (pos.getPositionEmpNo() == null)
				emptyPos = pos;
			else
				count++;
		}

		for (int i = 0; i < filePosList.size(); i++) {
			Position pos = (Position) filePosList.get(i);
			if (pos.getPositionPbId().getId().equals(currPB.getId()))
				count++;
		}

		if (currPB.getPbInCharge().intValue() == 1 && count > 0)
			return "Exceed";
		if (currPB.getPbMaxExceed().intValue() == 1
				&& currPB.getPbMaxEmployee() != null
				&& count + 1 > currPB.getPbMaxEmployee().intValue())
			return "Exceed";
		if (emptyPos == null) {
			emptyPos = new Position();
			emptyPos.setPositionPbId(currPB);
			emptyPos.setPositionParentId(null);
			emptyPos.setPositionTakeNum(new Integer(1));
		}
		emptyPos.setPositionEmpNo(emp);
		return emptyPos;
	}

	public List generateEmpConnTreeNodes() {
		List posList = findAllActivePosition();
		List nodeList = new ArrayList();
		for (int i = 0; i < posList.size(); i++) {
			Position pos = (Position) posList.get(i);
			TreeNode node = new TreeNode();
			node.setNodeId(pos.getId());
			node.setNodeParentId(pos.getPositionParentId() == null ? "0" : pos
					.getPositionParentId().getId());
			Employee emp = pos.getPositionEmpNo();
			if (emp != null) {
				node.setNodeName(emp.getEmpName());
				node.setEmpId(emp.getId());
				if (emp.getEmpGender().intValue() == 1)
					node.setNodeType(5);
				else
					node.setNodeType(6);
			} else {
				node.setNodeType(7);
				node.setNodeName("(\u7A7A\u7F3A)");
			}
			nodeList.add(node);
		}

		return nodeList;
	}

	public String judgePostion(String posId, Integer count) {
		Position pos = getPosById(posId, new String[] {
				Position.PROP_POSITION_EMP_NO, Position.PROP_POSITION_PB_ID });
		if (pos == null)
			return "\u53C2\u6570\u9519\u8BEF\uFF0C\u8BF7\u5237\u65B0\u540E\u91CD\u8BD5\uFF01";
		if (pos.getPositionPbId().getPbInCharge().intValue() == 1) {
			if (pos.getPositionEmpNo() != null
					&& !StringUtils.isEmpty(pos.getPositionEmpNo().getId()))
				return "\u8BE5\u8D1F\u8D23\u804C\u4F4D\u4E0D\u7A7A\u7F3A\uFF01";
			if (count.intValue() > 1)
				return "\u8D1F\u8D23\u804C\u4F4D\u5458\u5DE5\u5FC5\u987B\u552F\u4E00\uFF01";
		} else {
			IPositionBaseBo pbBo = (IPositionBaseBo) SpringBeanFactory
					.getBean("positionBaseBo");
			if (pbBo.checkPBExceed(pos.getPositionPbId(), count.intValue()))
				return "\u804C\u4F4D\u4EBA\u6570\u8D85\u8FC7\u7F16\u5236\uFF01";
		}
		return "SUCC";
	}

	public List searchPositionByEmpId(String empIds[]) {
		DetachedCriteria dc = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Position.class);
		dc.createAlias(Position.PROP_POSITION_EMP_NO, "positionEmpNo", 1);
		dc.createAlias((new StringBuilder()).append("positionEmpNo.").append(
				Employee.PROP_EMP_DEPT_NO).toString(), "empDeptNo", 1);
		dc.createAlias(Position.PROP_POSITION_PB_ID, "positionPbId", 1);
		dc.add(Restrictions
				.in((new StringBuilder()).append(Position.PROP_POSITION_EMP_NO)
						.append(".id").toString(), empIds));
		return positionDao.findByCriteria(dc);
	}

	public List searchSubPosition(String positionId[]) {
		DetachedCriteria dc = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Position.class);
		dc.createAlias(Position.PROP_POSITION_EMP_NO, "positionEmpNo", 1);
		dc.createAlias((new StringBuilder()).append("positionEmpNo.").append(
				Employee.PROP_EMP_DEPT_NO).toString(), "empDeptNo", 1);
		dc.createAlias(Position.PROP_POSITION_PB_ID, "positionPbId", 1);
		dc.add(Restrictions.in((new StringBuilder()).append(
				Position.PROP_POSITION_PARENT_ID).append(".id").toString(),
				positionId));
		return positionDao.findByCriteria(dc);
	}

	private IPositionDao positionDao;
}
